package com.iweb.oasys.servlet;

import com.iweb.oasys.service.UserService;
import com.iweb.oasys.service.UserServiceImpl;
import com.iweb.oasys.util.ResponseBody;
import com.iweb.oasys.util.ServletUtil;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet(name = "UserServlet",urlPatterns = "/user")
public class UserServlet extends HttpServlet {
    private UserService userService = UserServiceImpl.getInstance();

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request,response);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String action = request.getParameter("action");
        ResponseBody responseBody = null;
        try {
            switch (action) {
                case "login":
                    /* 把请求报文传给userService的login()方法处理,并且接收他返回的responseBody对象 */
                    responseBody = userService.login(request);
                    break;
                default:
                    break;
            }
        }catch (Exception e) {
            e.printStackTrace();
            responseBody.setResCode(1);
            responseBody.setErrMsg(e.getMessage());
        }
        /* 调用ServletUtil的returnResponse方法,返回响应报文 */
        ServletUtil.returnResponse(response,responseBody);

    }
}
